package com.example.a200peoject.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class DrawView extends View {


    private int curentX; //表示当前坐标X
    private int curentY; //表示当前坐标Y
    private Paint mPaint; //画笔

    public DrawView(Context context) {
        super(context);
        init();
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        curentX = 50;
        curentY = 100;
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE); //设置画笔格式
        mPaint.setColor(Color.RED); //设置画笔颜色
        mPaint.setStrokeWidth(2); //设置宽度
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(curentX, curentY, 15f, mPaint); //参数可以具体自己去看看
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        curentX = (int) event.getX(); //更新位置
        curentY = (int) event.getY();
        invalidate(); //重绘
        return true; //在此拦截，不向下传递
    }
}
